package com.lmh.system.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lmh.common.core.constants.CacheConstants;
import com.lmh.common.core.utils.TimeUtils;
import com.lmh.common.redis.service.RedisService;
import com.lmh.system.domain.exam.Exam;
import com.lmh.system.domain.exam.ExamQuestion;
import com.lmh.system.mapper.exam.ExamQuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Component
public class ExamCacheManager {
    @Autowired
    private RedisService redisService;

    @Autowired
    private ExamQuestionMapper examQuestionMapper;

    /**
     * 当管理员点击发布竞赛按钮的时候，就需要将竞赛的详细信息同步到redis中，并且将该竞赛的examId添加到对应的竞赛列表中
     * @param exam
     */
    public void addCache(Exam exam) {
        redisService.leftPushForList(getExamListKey(), exam.getExamId());
        redisService.setCacheObject(getDetailKey(exam.getExamId()), exam);
        redisService.addZSetObject(CacheConstants.EXAM_CONTINUE_START_TIME,exam.getExamId().toString()
                , (double) exam.getStartTime().toInstant(ZoneOffset.UTC).getEpochSecond());
        redisService.addZSetObject(CacheConstants.EXAM_CONTINUE_END_TIME,exam.getExamId().toString()
                , (double) exam.getEndTime().toInstant(ZoneOffset.UTC).getEpochSecond());
    }

    public void deleteCache(Long examId) {
        redisService.removeForList(getExamListKey(), examId);
        redisService.deleteObject(getDetailKey(examId));
        redisService.deleteObject(getExamQuestionListKey(examId));
        redisService.deleteZSetObject(CacheConstants.EXAM_CONTINUE_START_TIME,examId.toString());
        redisService.deleteZSetObject(CacheConstants.EXAM_CONTINUE_END_TIME,examId.toString());
    }

    public Long getExamQuestionListSize(Long examId) {
        String examQuestionListKey = getExamQuestionListKey(examId);
        return redisService.getListSize(examQuestionListKey);
    }

    public void refreshExamQuestionCache(Long examId) {
        List<ExamQuestion> examQuestionList = examQuestionMapper.selectList(new LambdaQueryWrapper<ExamQuestion>()
                .select(ExamQuestion::getQuestionId)
                .eq(ExamQuestion::getExamId,examId)
                .orderByAsc(ExamQuestion::getQuestionOrder));
        if (CollectionUtil.isEmpty(examQuestionList)) {
            return;
        }
        List<Long> questionIdList = examQuestionList.stream()
                .map(ExamQuestion::getQuestionId)
                .collect(Collectors.toList());
        redisService.rightPushAll(getExamQuestionListKey(examId),questionIdList);
        // 对竞赛题目列表设置过期时间为1天
        long seconds = ChronoUnit.SECONDS.between(LocalDateTime.now(),
                LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0));
        redisService.expire(getExamQuestionListKey(examId), seconds, TimeUnit.SECONDS);
    }


    private String getExamListKey() {
        return CacheConstants.EXAM_UNFINISHED_LIST;
    }

    private String getDetailKey(Long examId) {
        return CacheConstants.EXAM_DETAIL + examId;
    }

    private String getExamQuestionListKey(Long examId) {
        return CacheConstants.EXAM_QUESTION_LIST + examId;
    }
}
